﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
using BioBookClientSkole.ServiceReference1;


namespace BioBookClientSkole
{
    public class BioBookConnectionClass
    {
        static ServiceReference1.BioBookServiceClient BioBookService = new ServiceReference1.BioBookServiceClient();

        public BioBookConnectionClass()
        {

        }

        public void createEmployee(int phoneNumber, string email, string name, string password, string loginId)
        {

            BioBookService.createEmployee(phoneNumber, email, name, password, loginId);
        }

        public void createCustomer(int phoneNumber, string email, string name, string password)
        {

            BioBookService.createCustomer(phoneNumber, email, name, password);
        }

        public Person findPerson(int phoneNumber)
        {
            return BioBookService.findPerson(phoneNumber);
        }

        public void createShow(Movie movie, ShowRoom showRoom, int showId, string dt)
        {
            BioBookService.createShow(movie, showRoom, showId, dt);
        }

        public Show findShow(int showId)
        {
            return BioBookService.findShow(showId);
        }

        public Show UpdateShow(Show show)
        {
            return BioBookService.updateShow(show);
        }

        public void createMovie(string Title, string MovieLength, string MovieDescription)
        {
            BioBookService.createMovie(Title, MovieLength, MovieDescription);
        }

        public Movie findMovie(string Title)
        {
            return BioBookService.findMovie(Title);
        }

        public void createShowRoom(int showRoomNumber, int numberOfSeats, string showRoomDescription)
        {
            BioBookService.createShowRoom(showRoomNumber, numberOfSeats, showRoomDescription);
        }

        public ShowRoom findShowRoom(int showRoomNumber)
        {
            return BioBookService.findShowRoom(showRoomNumber);
        }

        public ShowRoom updateShowRoom(int showRoomNumber, int numberOfSeats, string showRoomDescription)
        {
            return BioBookService.updateShowRoom(showRoomNumber, numberOfSeats, showRoomDescription);
        }

        public void createReservation(Person p, Show show, List<Seat> seats, int reservationsID)
        {

            BioBookService.createReservation(p, show, seats, reservationsID);
        }

        public void deleteReservation(int reservationsID)
        {
            BioBookService.deleteReservation(reservationsID);
        }

        public List<Show> findAllShows()
        {
            //List<Show> show = new List<Show>();
            //foreach (Show s in BioBookService.findAllShows())
            //{
            //    int index = 0;
            //    int index2 = 0;
            //    if(BioBookService.findAllShows().ElementAt(index).showId > index2)
            //    {
            //        show.Add(s); 
            //    }

            //    else if(show == null && BioBookService.findAllShows().Count > index)
            //    {

            //        index++;
            //    }
            //}

            //return show;

            return BioBookService.findAllShows();
        }

        public List<Person> findAllPerson()
        {
            //List<Person> pers = new List<Person>();
            //foreach (Person p in BioBookService.findAllPerson())
            //{
            //    pers.Add(p);
            //}

            return BioBookService.findAllPerson();
        }

        public List<Reservation> findAllReservations()
        {
            //List<Reservation> res = new List<Reservation>();
            //foreach (Reservation r in BioBookService.findAllReservations())
            //{
            //    res.Add(r);
            //}
            return BioBookService.findAllReservations();
        }

        public List<Movie> findAllMovie()
        {
            //List<Movie> movie = new List<Movie>();
            //foreach (Movie m in BioBookService.findAllMovie())
            //{
            //    movie.Add(m);
            //}
            return BioBookService.findAllMovie();
        }

        public List<ShowRoom> findAllShowRooms()
        {
            return BioBookService.findAllShowRooms();
        }



        //public void serializePerson(FileStream fs)
        //{
        //    FileStream rs = new FileStream(@"http:\\www.fragportal.dk\grunk\", FileMode.Open);
        //    rs = BioBookService.SerializePerson(fs);
        //    XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());

        //    DataContractSerializer dSer = new DataContractSerializer(typeof(Person));
        //    Person p = (Person)dSer.ReadObject(reader,true);
        //    reader.Close();
        //    rs.Close();


        //}

        public object DeserializePerson(int tlf)
        {
            //string xml = "";
            ////Object p = new Object();
            //BinaryFormatter bin = new BinaryFormatter();
            //MemoryStream input = new MemoryStream(Encoding.UTF8.GetBytes(xml));
            //// memoryStream.Position = 0L;
            ////bin.Serialize(input, p);
            ////XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(input, Encoding.UTF8, new XmlDictionaryReaderQuotas(), null);

            ////DataContractSerializer dSer = new DataContractSerializer(typeof(Person));

            ////Person p = (Person)dSer.ReadObject(reader, true);
            //Person p = (Person)bin.Deserialize(input);
            //return p;

            //FileStream fs = new FileStream(fil, FileMode.OpenOrCreate);
            //Object p = new Object();

            //XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
            //DataContractSerializer dSer = new DataContractSerializer(typeof(Person));

            //class p = (Person)dSer.ReadObject(reader, true);
            //reader.Close();
            //fs.Close();

            //return p;

            //BinaryFormatter bin = new BinaryFormatter();
            //string fileName = "Person";
            //object p = BioBookService.findPerson(tlf);

            //using (Stream fStream = File.OpenRead(fileName))
            //{
            //    p = (Person)bin.Deserialize(fStream);
            //    //ph = p.getPhoneNumber;
            //    //email = p.getSetEmail;
            //    //loginid = p.getSetLoginId;
            //    //navn = p.getSetName;
            //    //pw = p.getSetPassword;
            //    //Type = p.getSetType;
            //    return p;
            //}

            ////Person dsP = new Person(ph, email, loginid, navn, pw, Type);

            //MemoryStream mem = new MemoryStream();
            //FileStream fsReader = new FileStream(fileName, FileMode.OpenOrCreate);


            DataContractSerializer serializer = new DataContractSerializer(typeof(Person));
            using (MemoryStream stream = new MemoryStream())
            {
                using (XmlReader xmlReader = XmlReader.Create(stream))
                {
                    Person p = (Person)serializer.ReadObject(xmlReader, true);
                    return p;
                }
            }




        }

    }
}
